//
//  KnowledgeTableView.m
//  yilutongxing
//
//  Created by ios-23 on 17/4/5.
//  Copyright © 2017年 YZE. All rights reserved.
//
#import "MJRefresh+FooterManger.h"
#import "KnowledgeTableView.h"
#import "YZECommunityTableViewCell4.h"
#import "KnowledgeDetailViewController.h"

#define PageUrl @"/api/transplant-knowledges?page="
#define URL_Topics @"/api/topics"

@implementation KnowledgeTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate=self;
        self.dataSource=self;
        self.backgroundColor = WHITE_COLOR;
        self.showsVerticalScrollIndicator = NO;
        
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerNib:[UINib nibWithNibName:@"YZECommunityTableViewCell4" bundle:[NSBundle mainBundle]]
   forCellReuseIdentifier:@"YZECommunityTableViewCell4"];
        
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
        view.backgroundColor = [UIColor colorWithRed:238/250.0 green:238/250.0 blue:238/250.0 alpha:1];
        
        self.tableHeaderView=view;
        self.mj_header=[RefershHelper headerWithRefreshingBlock:^{
            [[YZENetwork sharedInstace]getRequestWithUrl:[BASE stringByAppendingString:PageUrl] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
                self.knowledgeArr=[YZEModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
                [self reloadData];
                self.footRefreshState = MJFooterRefreshStateLoadMore;
            }];
            
            [self.mj_header endRefreshing];
        }];
        __block KnowledgeTableView *Knowledge=self;
        self.pagNum = 0;
        self.mj_footer=[RefershHelper footerWithRefreshingBlock:^{
            Knowledge.pagNum ++;
            [[YZENetwork sharedInstace]getRequestWithUrl:[NSString stringWithFormat:@"%@%@%ld",BASE,PageUrl,(long)Knowledge.pagNum] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
                if (responseObject == nil) {
                    self.footRefreshState = MJFooterRefreshStateNoMore;
                }
                NSMutableArray *arr=[YZEModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
                [Knowledge.knowledgeArr addObjectsFromArray:arr];
                [self reloadData];
            }];
            self.footRefreshState = MJFooterRefreshStateLoadMore;
        }];
        
    }
    return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.knowledgeArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZECommunityTableViewCell4 *cell = [tableView dequeueReusableCellWithIdentifier:@"YZECommunityTableViewCell4" forIndexPath:indexPath];
    YZEModel *model=self.knowledgeArr[indexPath.section];
    cell.titileLable.text=model.name;
    cell.creatDataLable.text=model.createdDate;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    KnowledgeDetailViewController *detail=[KnowledgeDetailViewController new];
    UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:detail];
    YZEModel *model=self.knowledgeArr[indexPath.section];
    [detail getWebID:model.ID];
    [[self viewController] presentViewController:navi animated:YES completion:nil];
    
}
- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
@end
